<?php
/**
* @version $Id: flytranslate.php 5203 2011-01-20 02:45:14Z  $
* @package Joomla 1.5
* @copyright Copyright (C) 2011 Miguel Puig. All rights reserved.
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
* Select a text on your joomla web page and Automatically translates via Google Translate
*/

 error_reporting(E_ALL);
 ini_set("display_errors", 1); 


defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.event.plugin');

class plgContentFlytranslate extends JPlugin{

    function __getCatID()
    {

		  $db = &JFactory::getDBO();            
		  $option = JRequest::getCmd('option');
		  $view = JRequest::getCmd('view');
		  $temp = JRequest::getString('id');
		  $temp = explode(':', $temp);
		  $id = $temp[0];
		  if ($option == 'com_content' && $id)
		  {
		    /* Checking if we are making up an article page */
		    if ($view == 'article')
		    {            
		      /* Trying to get CATEGORY title from DB */
		      $db->setQuery('SELECT cat.id FROM #__categories cat RIGHT JOIN #__content cont ON cat.id = cont.catid WHERE cont.id='.$id);   
		      $category_title = $db->loadResult();
		    }
		    /* Checking if we are making up a category page */
		    if ($view == 'category')
		    {            
		      /* Trying to get CATEGORY title from DB */
		      $db->setQuery('SELECT cat.id FROM #__categories cat WHERE cat.id='.$id);   
		      $category_title = $db->loadResult();
		    }
		    /* Printing category title*/
		    if ($category_title) 
		    {
		             
		    }
		  }  
		  
		  return $category_title; 
				    
    }
    
    
    function __getCurrentLangTag(){
	    
	    $lang =& JFactory::getLanguage();
		
		$langtag = $lang->getTag();
		
		$langtag = strstr($langtag, '-', true); 

	    return $langtag;
    }
    
    function plgContentFlytranslate (&$subject)
    {
            parent::__construct ($subject);
    
    }
    
    
    function onPrepareContent (&$article, &$params, $page=0) 
    {
    
		$plugin =& JPluginHelper::getPlugin('content', 'flytranslate');
   		$pluginParams = new JParameter( $plugin->params );



$langs = array();

for($i=1; $i<5; $i++){
	
	$ids = $pluginParams->get('cats'.$i,'');
	$name = strstr($pluginParams->get('lang'.$i,''), '-', true); 
	
	if($ids):
	
		$pieces = explode(",", $ids);
		
		$langs[$i]['ids'] = $pieces;
		$langs[$i]['name'] = $name;
		if (in_array($this->__getCatID(), $pieces)) {
		    	$FromLang = $name;


		}
		

		
	endif;
	

}

if(!isset($FromLang)){
	echo "Language not found, please add a category id to any languageon the parameters of the plugin Fly translate";
}


else{


	
		echo "Curr catID: ".$this->__getCatID();
		echo "<br />Curr Lang FROM: ".$FromLang;
		echo "<br />Curr Lang TO: ".$this->__getCurrentLangTag();	
		

            	if (!$pluginParams->get('replace_limit', 1)) {
                	 $num = $pluginParams->get('replace_limit', 1);
           		}
           		
           		$translate_text = $pluginParams->get('translate_text','translating...');
           		
           		$language_from = $FromLang;
           		$language_to = $this->__getCurrentLangTag();
           		

                $document	= JFactory::getDocument();
  				
				$document->addScript(JURI::base() . 'plugins/content/flytranslate/jquery.min.js');
				$document->addScript(JURI::base() . 'plugins/content/flytranslate/jquery.qtip.js');
        		
        		
        		$document->addStyleSheet(JURI::base() . 'plugins/content/flytranslate/master.css');
        		$document->addStyleSheet(JURI::base() . 'plugins/content/flytranslate/jquery.qtip.css');
        		$document->addStyleSheet(JURI::base() . 'plugins/content/flytranslate/demos.css');
        		

  
				
				$formated_text = '<div class="translate">';
				$formated_text .= $article->text;
				$formated_text .= '</div>';
				$formated_text .= '<script type="text/javascript" src="http://www.google.com/jsapi"></script>';
				$formated_text .= $this->render_js($language_from,$language_to,$translate_text);

				$article->text = $formated_text;

}				
           
    }
    
    private function render_js($language_from,$language_to,$translate_text)
    {

$js = '<script type="text/javascript">
// Initialize google API
google.load("language", "1");

// Setup the selection grabber function
function getSelected()
{
	if(window.getSelection) { txt = window.getSelection().toString(); }
	else if(document.getSelection) { txt = document.getSelection().toString(); }
	else if(document.selection) { txt = document.selection.createRange().text; }

	return txt;
}

// Setup a single tooltip on the document body element
$(document.body).qtip(
{
	prerender: true, // Make sure its rendered at document load
	content: "'.$translate_text.'",
	position: {
		my: "bottom right",
		at: "top left",
		viewport: $(window), // Keep it on screen if possible
		target: "mouse", // Position it where the mouse up event happens...
		adjust: { mouse: false } // ... but don"t follow the mouse
	},
	show: false, // Disable the default show event
	hide: "unfocus", // Hide the tooltip when it loses focus
	style: {
		classes: "ui-tooltip-translate ui-tooltip-shadow"
	}
})

.bind("mouseup", function(event) {
	// Grab the text that is selected
	var text = getSelected();

	// If the qTip was the target, hide the tooltips
	if(text.length)
	{
		// Retrieve API
		var api = $(this).qtip();

		// Set translating... as content and show the tooltip
		api.set("content.text", "'.$translate_text.'");
		api.show(event); // ..make sure to pass the event object!

		// Translate the text and set new content
		google.language.translate(text, "'.$language_from.'", "'.$language_to.'", function(result){
			api.set("content.text", result.translation);
		});
	}
});</script>';

return $js;  
    
    }

    
  }
  